package com.kt.uokos.yimo.controller;

import com.kt.uokos.yimo.anno.validator.Order;
import com.kt.uokos.yimo.anno.validator.Sort;
import com.kt.uokos.yimo.dao.Issue;
import com.kt.uokos.yimo.service.IssueService;
import com.kt.uokos.yimo.util.ResponseUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/issue")
public class IssueController {

    @Resource
    private IssueService issueService;

    @GetMapping("/list")
    public Object list(String question, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, @Sort @RequestParam(defaultValue = "add_time") String sort, @Order @RequestParam(defaultValue = "desc") String order) {
        List<Issue> issues = issueService.querySelective(question, page, limit, sort, order);
        return ResponseUtil.okList(issues);
    }

    @PostMapping("/create")
    public Object add(@RequestBody Issue issue) {
        if (StringUtils.isEmpty(issue.getQuestion())) {
            return ResponseUtil.errs();
        }
        if (StringUtils.isEmpty(issue.getAnswer())) {
            return ResponseUtil.errs();
        }
        issueService.add(issue);
        return ResponseUtil.ok(issue);
    }

    @PostMapping("/update")
    public Object update(@RequestBody Issue issue){
        issueService.update(issue);
        return ResponseUtil.ok();
    }

    @PostMapping("/delete")
    public Object delete(@RequestBody Issue issue){
        if (issue.getId() == null){
            return ResponseUtil.errs();
        }
        issueService.delete(issue);
        return ResponseUtil.ok();
    }
}